package org.cardoso.coyote.persistence;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

public class PersistenceManagerFactory {

	private static PersistenceManagerFactory persistenceManagerfactory;
	private EntityManagerFactory entityManagerFactory;

	private PersistenceManagerFactory() {
	}

	public static EntityManagerFactory currentEntityManagerFactory() {
		if (persistenceManagerfactory == null) {
			initialize();
			createEntityManagerFactory();
		}
		return persistenceManagerfactory.getEntityManagerFactory();
	}

	private static void createEntityManagerFactory() {
		persistenceManagerfactory.setEntityManagerFactory(javax.persistence.Persistence.createEntityManagerFactory("default"));
	}

	public static EntityManager currentEntityManager() {
		return currentEntityManagerFactory().createEntityManager();
	}

	private static void initialize() {
		persistenceManagerfactory = new PersistenceManagerFactory();
	}

	private void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
		this.entityManagerFactory = entityManagerFactory;
	}

	private EntityManagerFactory getEntityManagerFactory() {
		return this.entityManagerFactory;
	}

}